package com.txm.qqcommon;

import com.txm.service.ClientConnectServerThread;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
/**
* 用户发送文件
* */
public class TransferFiles extends Thread {
    JButton openFile;
    String filePath = "";
    File file;
    JFrame jFrame;
    String getter;
    ClientConnectServerThread ccst;

    public TransferFiles(String getter, ClientConnectServerThread ccst) {
        System.out.println("请选择传输的文件...");
        this.getter = getter;
        this.ccst = ccst;
        runFileChooser();

        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(ccst.getSocket().getOutputStream());
            Message message = new Message();
            message.setContent(file.getName());
            message.setGetter(getter);
            message.setSender(ccst.user.getUserId());
            message.setMsgType(MessageType.MESSAGE_SENDER_FILE_USER);
//            System.out.println(message);
            objectOutputStream.writeObject(new Response("/file", message));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void run() {
        System.out.println("开始传输文件");
        try {
            byte[] bytes = new byte[1024];
            int readInt = 0;

            FileInputStream fis = new FileInputStream(file);
            Message message = new Message();
            message.setSender(ccst.user.getUserId());
            message.setGetter(getter);
            message.setMsgType(MessageType.MESSAGE_SENDER_FILE_ING);
            File_ file1 = new File_(file);

            while ((readInt = fis.read(bytes)) != -1) {
                message.setCurrentLocalDate();
                ObjectOutputStream oos = new ObjectOutputStream(ccst.getSocket().getOutputStream());
                file1.setDataCut(bytes);
                file1.setDataCut_size(readInt);
                message.setObject(file1);
//                System.out.println(message);
                oos.writeObject(new Response("/fileUploading", message));
                System.out.println(file1.getProgress());
            }
            fis.close();
            System.out.println("文件已传输至服务器。");
            ccst.qqView.getMainView();
        } catch (
                IOException e) {
            throw new RuntimeException(e);
        }

    }

    public void runFileChooser() {
        jFrame = new JFrame();
        openFile = new JButton("Open File");
        openFile.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                createAndShowFileChooser();
            }
        });
        JPanel panel = new JPanel();
        panel.add(openFile);
        jFrame.getContentPane().add(panel);

        jFrame.setSize(300, 200);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setLocationRelativeTo(null);
        jFrame.setResizable(false);
        jFrame.setVisible(true);
        openFile.doClick();
    }


    public void createAndShowFileChooser() {
        // 在事件调度线程中创建和显示JFileChooser
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setToolTipText("选择文件");
        int returnValue = fileChooser.showOpenDialog(jFrame); // null表示没有父组件
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            // 用户选择了文件
            System.out.println("Selected file: " + fileChooser.getSelectedFile().getAbsolutePath());
            jFrame.setVisible(false);
            filePath = fileChooser.getSelectedFile().getAbsolutePath();
            file = new File(filePath);
        } else {
            // 用户取消了选择
            System.out.println("File selection cancelled.");
            jFrame.setVisible(false);
            ccst.qqView.getMainView();
        }
    }

}
